//
//  MYGMyReportsNewTableViewCell.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/5/6.
//

import UIKit

class MYGMyReportsNewTableViewCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        contentView.backgroundColor = .clear
        initUIs()
    }
    
    var tapBoxCompletion:((MYGArticleModel)->Void)?
    private func initUIs(){
        contentView.addSubviews([selectedBtn,titleLab,timeLab,contentImageV,fromImageLabel,fromLabel,starBtn,starsNumLabel,shareBtn,sharesNumLabel,lineView])
        selectedBtn.snp.makeConstraints { make in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo(49)
        }
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(selectedBtn.snp.right).offset(15)
            make.top.equalToSuperview().offset(21)
            make.right.equalToSuperview().offset(-121)
            make.height.equalTo(39)
        }
        contentImageV.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-12)
            make.top.equalToSuperview().offset(20)
            make.size.equalTo(CGSize(width: 100, height: 87.5))
        }
        timeLab.snp.makeConstraints { make in
            make.left.equalTo(selectedBtn.snp.right).offset(15)
            make.top.equalTo(titleLab.snp.bottom).offset(7)
            make.height.equalTo(16.5)
        }
        fromImageLabel.snp.makeConstraints { make in
            make.left.equalTo(selectedBtn.snp.right).offset(15)
            make.top.equalTo(timeLab.snp.bottom).offset(8)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        fromLabel.snp.makeConstraints { make in
            make.left.equalTo(fromImageLabel.snp.right).offset(6)
            make.centerY.equalTo(fromImageLabel)
            make.height.equalTo(13)
        }
        sharesNumLabel.snp.makeConstraints { make in
            make.right.equalTo(contentImageV.snp.left).offset(-12)
            make.centerY.equalTo(fromImageLabel)
            make.height.equalTo(15)
        }
        shareBtn.snp.makeConstraints { make in
            make.right.equalTo(sharesNumLabel.snp.left).offset(-4.5)
            make.centerY.equalTo(fromImageLabel)
            make.size.equalTo(CGSize(width: 20, height: 30))
        }
        starsNumLabel.snp.makeConstraints { make in
            make.right.equalTo(shareBtn.snp.left).offset(-7.5)
            make.centerY.equalTo(fromImageLabel)
            make.height.equalTo(15)
        }
        starBtn.snp.makeConstraints { make in
            make.right.equalTo(starsNumLabel.snp.left).offset(-4.5)
            make.centerY.equalTo(fromImageLabel)
            make.size.equalTo(CGSize(width: 20, height: 30))
        }
        lineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    var model : MYGArticleModel = MYGArticleModel() {
        didSet {
            let btnWidtg = model.isEdit ? 49: 0
            selectedBtn.snp.updateConstraints { make in
                make.width.equalTo(btnWidtg)
            }
            titleLab.text = model.title
            timeLab.text = model.created_at
            contentImageV.sd_setImage(with: URL(string: model.img), placeholderImage: UIImage.init(named: "head"), context: nil)
            fromLabel.text = model.from
            starsNumLabel.text = "\(model.collect_count)"
            sharesNumLabel.text = "\(model.share_count)"
            fromImageLabel.text = String(model.from.prefix(1))
            selectedBtn.isSelected = model.isSelected
            starBtn.isSelected = model.collect_count > 0
        }
    }

 
    private lazy var titleLab:UILabel = {
        let titleLab = UILabel()
        titleLab.textColor = .rgba(51, 51, 51)
        titleLab.font = .medium(15)
        titleLab.text = "北京安贞医院成功完成国内首例主动脉瓣临床治疗治疗"
        titleLab.numberOfLines = 2
        return titleLab
    }()
    private lazy var timeLab:UILabel = {
        let timeLab = UILabel()
        timeLab.textColor = .rgba(128, 128, 128)
        timeLab.font = .regular(12)
        timeLab.text = "2023-09-09"
        return timeLab
    }()
    private lazy var contentImageV:UIImageView = {
        let cont = UIImageView()
        cont.contentMode = .scaleAspectFill
        cont.layer.masksToBounds = true
        cont.layer.cornerRadius = 5
        return cont
    }()
    private lazy var fromImageLabel:UILabel = {
        let cont = UILabel()
        cont.backgroundColor = .red
        cont.font = .regular(9)
        cont.textAlignment = .center
        cont.textColor = .white
        cont.layer.masksToBounds = true
        cont.layer.cornerRadius = 7.5
        return cont
    }()
    private lazy var fromLabel:UILabel = {
        let cont = UILabel()
        cont.textColor = .rgba(128, 128, 128)
        cont.font = .regular(13)
        cont.text = "北京新闻出版社"
        return cont
    }()
    private lazy var starsNumLabel:UILabel = {
        let cont = UILabel()
        cont.textColor = .rgba(171, 171, 171)
        cont.font = .regular(11)
        cont.text = "123"
        return cont
    }()
    private lazy var sharesNumLabel:UILabel = {
        let cont = UILabel()
        cont.textColor = .rgba(171, 171, 171)
        cont.font = .regular(11)
        cont.text = "123"
        return cont
    }()
    private lazy var starBtn:UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "report_starUnselect"), for: .normal)
        cont.setImage(UIImage(named: "report_starSelect"), for: .selected)
        cont.addTarget(self, action: #selector(tapCollectionAction), for: .touchUpInside)
        return cont
    }()
    private lazy var shareBtn:UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "report_share"), for: .normal)
        cont.addTarget(self, action: #selector(tapShare), for: .touchUpInside)
        return cont
    }()
    private lazy var lineView:UIView = {
        let cont = UIView()
        cont.backgroundColor = RGBA(r: 238, g: 238, b: 238, a: 1)
        return cont
    }()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var selectedBtn:UIButton = {
        let control = UIButton()
        control.setImage(UIImage.init(named: "MYG_report_boxnormal"), for: .normal)
        control.setImage(UIImage.init(named: "MYG_report_boxselected"), for: .selected)
        control.setImage(UIImage.init(named: "MYG_report_boxselected"), for: .highlighted)
        control.addTarget(self, action: #selector(tapBox(sender:)), for: .touchUpInside)
        return control
    }()
    
    @objc func tapBox(sender:UIButton) {
        if let c = tapBoxCompletion {
            c(self.model)
        }
    }
    
    @objc func tapShare() {
        let textToShare:String = "医路通"
        let imageToShare:UIImage = UIImage(named: "AppIcon")!
        let reportId = self.model.id
        let token = MYGUserManager.getUser()?.verification_token ?? ""
        let urlToShare:URL = URL(string: "\(shareUrl)/#/pages/shareH5/report?id=\(reportId)&token=\(token)")!
        
        let activityItems:Array = [textToShare, imageToShare, urlToShare] as [Any]
        let activityVC:UIActivityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)
        activityVC.excludedActivityTypes = [UIActivity.ActivityType.postToFacebook,UIActivity.ActivityType.airDrop]
        
        self.rootResponsder().navigationController?.present(activityVC, animated: true)
    }
    
    @objc func tapCollectionAction() {
        var collectionStatus = "1"
        if self.model.collect_count <= 0 {
            collectionStatus = "1"
            self.model.is_collect = 1
            model.collect_count = model.collect_count + 1
        }else {
            collectionStatus = "0"
            self.model.is_collect = 0
            model.collect_count = model.collect_count - 1
        }
        self.starBtn.isSelected = self.model.is_collect == 1
        
        starsNumLabel.text = "\(max(0, model.collect_count))"

        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .collectSubmit(param: [
            "data_id":"\(self.model.id)",
           "type":"6",
            "collect_status":collectionStatus])) { item in

            } failed: { _, error in
           MMYGHUD.showToast(text: error)
       }
    }
}
